package org.dreamwork.tools.network.bridge.client.fx.controllers;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.Tooltip;
import javafx.stage.Modality;
import javafx.stage.Stage;
import org.dreamwork.network.service.ServiceFactory;
import org.dreamwork.network.sshd.data.User;
import org.dreamwork.tools.network.bridge.client.data.Proxy;
import org.dreamwork.tools.network.bridge.client.fx.DialogHelp;
import org.dreamwork.tools.network.bridge.client.fx.GuiContext;
import org.dreamwork.tools.network.bridge.client.fx.StageManager;
import org.dreamwork.tools.network.bridge.client.fx.views.ProxyCell;
import org.dreamwork.tools.network.bridge.client.services.IClientMonitorService;
import org.dreamwork.tools.network.bridge.client.services.IClientService;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

/**
 * Created by seth.yang on 2020/3/10
 */
public class MainController extends AbstractController {
    @FXML private Button btnServer;
    @FXML private Button btnNewTunnel;
    @FXML private Button btnRemove;
    @FXML private Button btnEdit;
    @FXML private Button btnConnect;
    @FXML private ListView<Proxy> proxies;

    @Override
    public void initialize (URL location, ResourceBundle resources) {
        super.initialize (location, resources);

        proxies.setCellFactory (param -> new ProxyCell ());
        btnServer.setTooltip (new Tooltip (getResource ("tips.server.manage")));
        btnNewTunnel.setTooltip (new Tooltip (getResource ("tips.create.tunnel")));
        btnRemove.setTooltip (new Tooltip (getResource ("tips.remove.tunnel")));
        btnEdit.setTooltip (new Tooltip (getResource ("tips.edit.tunnel")));
        btnConnect.setTooltip (new Tooltip (getResource ("tips.connect.tunnel")));
        proxies.setOnMouseClicked (e->{
            Proxy proxy = proxies.getSelectionModel ().getSelectedItem ();
            if (proxy != null) {
                btnConnect.setDisable (false);

                IClientMonitorService monitor = ServiceFactory.get (IClientMonitorService.class);
                if (monitor.containsClient (proxy.name)) {
                    // the proxy already connected
                    btnEdit.setDisable (true);      // cannot edit
                    btnRemove.setDisable (true);    // cannot remove

                    btnConnect.getStyleClass ().remove ("connect-button");
                    btnConnect.getStyleClass ().add ("disconnected-button");
                } else {
                    btnEdit.setDisable (false);
                    btnRemove.setDisable (false);

                    btnConnect.getStyleClass ().add ("connect-button");
                    btnConnect.getStyleClass ().remove ("disconnected-button");
                }
            } else {
                btnEdit.setDisable (true);
                btnRemove.setDisable (true);
                btnConnect.setDisable (true);
            }
        });

        background (()->{
            IClientService service = ServiceFactory.get (IClientService.class);
            List<Proxy> list = service.getProxies ();
            post (() -> {
                proxies.getItems ().clear ();
                proxies.getItems ().addAll (list);
            });
        });
    }

    @Override
    public void setStage (Stage stage, String title) {
        super.setStage (stage, title);
    }

    @FXML private void onSetting () {
        User user = GuiContext.getCurrentUser ();
        if (user != null) {

        } else {
            try {
                StageManager.Holder<LoginController> holder =
                        StageManager.showStage ("jfx/login.fxml", GuiContext.ui, Modality.APPLICATION_MODAL);
                if (holder != null) {
                    holder.controller.setStage (holder.stage, "User Login");
                }
            } catch (IOException ex) {
                DialogHelp.error (ex.getMessage (), ex);
            }
        }
    }

    @FXML private void onServerClick () {
        StageManager.showStage ("jfx/server-manager.fxml", GuiContext.ui, "Server Manager", false);
    }

    @FXML private void createTunnel () {
        StageManager.showStage ("jfx/tunnel.fxml", GuiContext.ui, getResource ("title.tunnel.manage"), false);
    }

    @FXML private void removeTunnel () {

    }

    @FXML private void editTunnel () {
        Proxy proxy = proxies.getSelectionModel ().getSelectedItem ();
        if (proxy != null) {
            StageManager.Holder<TunnelController> holder =
                    StageManager.showStage (
                            "jfx/tunnel.fxml",
                            GuiContext.ui, getResource ("title.tunnel.manage"),
                            false
                    );
            if (holder != null && holder.controller != null) {
                holder.controller.setProxy (proxy);
            }
        }
    }

    @FXML private void connect () {}
}